{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Instructions\n",
    "\n",
    "Write a function that, given a list of x-axis intervals, relative probabilities and a total probability, calculates the height of each bar. Remember that the sum of the area for all bars should be the total probability.\n",
    "\n",
    "Here is an example input and output:\n",
    "* a vehicle accident is 5 times more likely from 5am to 10am versus midnight to 5am. \n",
    "* a vehicle accident is 3 times more likely from 10am to 4pm versus midnight to 5am.\n",
    "* a vehicle accident is 6 times more likely from 4pm to 9pm versus midnight to 5am.\n",
    "* a vehicle accident is 1/2 as likely from 9pm to midnight versus midnight to 5am.\n",
    "* The probability of getting in an accident on any given day is .05\n",
    "\n",
    "The inputs would look like this. \n",
    "\n",
    "For the hours, you can use 24 hour time\n",
    "hour_intervals = [0, 5, 10, 16, 21, 24]\n",
    "\n",
    "relative_probabilities = [1, 5, 3, 6, 0.5]\n",
    "\n",
    "total_probability = 0.05\n",
    "\n",
    "The output would be the height of each bar:\n",
    "```\n",
    "[0.0006451612903225806,\n",
    " 0.0032258064516129032,\n",
    " 0.0016129032258064516,\n",
    " 0.003870967741935484,\n",
    " 0.0005376344086021505]\n",
    "```\n",
    "\n",
    "At the end of the exercise, your visualization should look like this:\n",
    "\n",
    "![piece-wise continuous visualization](piece-wise-function.png)\n",
    "\n",
    "# Hints\n",
    "\n",
    "* Summing the area of all the bars equals the total probability, which in this case is 0.05.\n",
    "* The relative probabilities and total probability can be used to find the exact area of each bar. If the area of the first bar is A, then the area of the second bar is 5A, the third bar is 3A, etc.\n",
    "* Once you know the area of each bar, you can divide each area by its width to calculate the bar height."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Function Inputs and Outputs\n",
    "\n",
    "The bar_heights() function below has three inputs\n",
    "* intervals - representing the x-axis intervals for each bar\n",
    "* relative_probabilities - representing the relative probabilities for each interval\n",
    "* total_probability - representing the total area of all the bars\n",
    "\n",
    "The bar_heights() function as one output\n",
    "* heights - a list of each heigt for each interval in the probability density function\n",
    "\n",
    "Fill in the TODOs to get the function working"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "def bar_heights(intervals, relative_probabilities, total_probability):\n",
    "\n",
    "    heights = []\n",
    "    \n",
    "    #TODO: sum the relative probabilities\n",
    "    total_relative_prob = 0\n",
    "    \n",
    "    for i in range(0, len(relative_probabilities)):\n",
    "        \n",
    "        #TODO: Looping through the relative_probabilities list, \n",
    "        #      take one probability at a time and \n",
    "        #      calculate the area of each bar. Think about how you can \n",
    "        #      calculate the area of a bar knowing the total_probability,\n",
    "        #      relative probability, and the sum of the relative probabilities.\n",
    "        #      这里 interval 不报错的原因是 interval 元素有6个， relative_probabilities 有5个\n",
    "        bar_width = intervals[i+1] - intervals[i]\n",
    "        #HINT: It's possible to do this in one line of code\n",
    "        \n",
    "        bar_area = relative_probabilities[i]\n",
    "        \n",
    "        # TODO: Calculate the height of the bar and append the value to the\n",
    "        # heights list. Remember that the area of each bar \n",
    "        # is the width of the bar times the height of the bar\n",
    "        \n",
    "        #HINT: It's possible to do this in one line of code\n",
    "        heights.append(bar_area * 1.0 / bar_width)\n",
    "        \n",
    "    return heights"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run the next cell to test out your function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.2, 1.0, 0.5, 1.2, 0.16666666666666666]\n"
     ]
    }
   ],
   "source": [
    "print(bar_heights([0, 5, 10, 16, 21, 24], [1, 5, 3, 6, 0.5], 0.05))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Visualize Results\n",
    "\n",
    "Once the bar_heights function is working, here is some code to visualize your results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "hour_intervals = [0, 5, 10, 16, 21, 24]\n",
    "probability_intervals = [1, 5, 3, 6, 1/2]\n",
    "accident_probability = 0.05\n",
    "\n",
    "heights = bar_heights(hour_intervals, probability_intervals, accident_probability)\n",
    "\n",
    "for i in range(len(probability_intervals)):\n",
    "    plt.plot([hour_intervals[i], hour_intervals[i+1]], [heights[i], heights[i]], color='blue') \n",
    "    plt.plot([hour_intervals[i], hour_intervals[i]], [0, heights[i]], '--', color='blue')\n",
    "    plt.plot([hour_intervals[i+1], hour_intervals[i+1]], [0, heights[i]], '--', color='blue')\n",
    "\n",
    "plt.xticks(range(0,25,1))\n",
    "plt.xlabel('hours')\n",
    "plt.ylabel('probability density function')\n",
    "plt.title('Probability Density Function \\n for Car Accidents During the Day')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When you run the code cell above, the visualization should look like this:\n",
    "\n",
    "![piece-wise continuous visualization](piece-wise-function.png)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
